<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始日期" prop="startDate">
        <el-date-picker
          clearable
          v-model="queryParams.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="endDate">
        <el-date-picker
          clearable
          v-model="queryParams.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="是否公开" prop="isPublic">
        <el-select filterable v-model="queryParams.isPublic" placeholder="请选择是否公开">
          <el-option
            v-for="dict in dict.type.is_public"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="人才类型" prop="talentLevel">
        <el-input
          v-model="queryParams.talentLevel"
          placeholder="请选择人才类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名人数限制" prop="enrollmentLimit">
        <el-input
          v-model="queryParams.enrollmentLimit"
          placeholder="请输入报名人数限制"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['event:event:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['event:event:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['event:event:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['event:event:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="eventList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="title" />
      <!-- <el-table-column label="描述" align="center" prop="description" /> -->
      <el-table-column label="开始日期" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名截止日期" align="center" prop="registrationDeadline" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registrationDeadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名人数" align="center" prop="registrationCount" />
      <el-table-column label="参加条件" align="center" prop="condition" />
      <el-table-column label="居住地" align="center" prop="residential" />
      <el-table-column label="是否公开" align="center" prop="isPublic">
        <template slot-scope="scope">
          {{ getValue(dict.type.is_public, scope.row.isPublic) }}
        </template>
      </el-table-column>
      <el-table-column label="人才层次" align="center" prop="talentLevel" />
      <el-table-column label="居住地区" align="center" prop="residenceArea" />
      <el-table-column label="报名人数限制" align="center" prop="enrollmentLimit" />
      <el-table-column label="审批状态" align="center" prop="approvalStatus" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['event:event:edit']"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['event:event:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker
            clearable
            v-model="form.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker
            clearable
            v-model="form.endDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="报名截止日期" prop="registrationDeadline">
          <el-date-picker
            clearable
            v-model="form.registrationDeadline"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报名截止日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="报名人数" prop="registrationCount">
          <el-input v-model="form.registrationCount" placeholder="请输入报名人数" />
        </el-form-item>
        <el-form-item label="参加条件" prop="condition">
          <el-input v-model="form.condition" placeholder="请输入参加条件" />
        </el-form-item>
        <el-form-item label="文件名" prop="fileName">
          <FileUpload
            :value="form.fileName"
            @input="changeInput('fileName', arguments)"
          ></FileUpload>
        </el-form-item>
        <!-- <el-form-item label="图片名" prop="imageName">
          <FileUpload :value="form.imageName" @input="changeInput('imageName', arguments)"></FileUpload>
        </el-form-item>
        <el-form-item label="视频名" prop="videoName">
          <FileUpload :value="form.videoName" @input="changeInput('videoName', arguments)"></FileUpload>
        </el-form-item> -->
        <el-form-item label="是否公开" prop="isPublic">
          <el-select filterable v-model="form.isPublic" placeholder="请选择是否公开">
            <el-option
              v-for="dict in dict.type.is_public"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="人才层次" prop="talentLevel">
          <el-select filterable v-model="form.talentLevel" placeholder="请选择人才层次">
            <el-option
              v-for="dict in dict.type.elite_type"
              :key="dict.value"
              :label="dict.label + dict.raw.remark"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="居住地区" prop="residenceArea">
          <el-select filterable v-model="form.residenceArea" placeholder="请选择居住地区">
            <el-option
              v-for="dict in dict.type.residence_area"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报名人数限制" prop="enrollmentLimit">
          <el-input v-model="form.enrollmentLimit" placeholder="请输入报名人数限制" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEvent, getEvent, delEvent, addEvent, updateEvent } from '@/api/event/event'

export default {
  name: 'Event',
  dicts: ['is_public', 'residence_area', 'elite_type'],
  data() {
    return {
      files: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动表格数据
      eventList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        description: null,
        startDate: null,
        endDate: null,
        registrationDeadline: null,
        registrationCount: null,
        condition: null,
        residential: null,
        fileName: null,
        imageName: null,
        videoName: null,
        isPublic: null,
        talentLevel: null,
        residenceArea: null,
        enrollmentLimit: null,
        approvalStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fileName: [{ required: true, message: '请选取文件', trigger: 'change' }],
        imageName: [{ required: true, message: '请选取文件', trigger: 'change' }],
        videoName: [{ required: true, message: '请选取文件', trigger: 'change' }],
        isPublic: [{ required: true, message: '请选择是否公开', trigger: 'change' }],
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        startDate: [{ required: true, message: '请选择开始日期', trigger: 'change' }],
        endDate: [{ required: true, message: '请选择结束日期', trigger: 'change' }],
        registrationDeadline: [
          { required: true, message: '请选择报名截止日期', trigger: 'change' }
        ],
        registrationCount: [{ required: true, message: '报名人数不能为空', trigger: 'blur' }],
        condition: [{ required: true, message: '请选择参加条件', trigger: 'change' }],
        talentLevel: [{ required: true, message: '请选择人才层次', trigger: 'change' }],
        residenceArea: [{ required: true, message: '请选择居住地区', trigger: 'change' }],
        enrollmentLimit: [{ required: true, message: '报名人数限制不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    changeInput(fileName, files) {
      this.$set(this.form, fileName, files[1])
    },
    getValue(arr, value) {
      let item = arr.find((item) => item.value == value)
      return item.label
    },
    /** 查询活动列表 */
    getList() {
      this.loading = true
      listEvent(this.queryParams).then((response) => {
        this.eventList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        eventId: null,
        title: null,
        description: null,
        startDate: null,
        endDate: null,
        registrationDeadline: null,
        registrationCount: null,
        condition: null,
        residential: null,
        fileName: null,
        imageName: null,
        videoName: null,
        isPublic: null,
        talentLevel: null,
        residenceArea: null,
        enrollmentLimit: null,
        approvalStatus: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.eventId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加活动'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const eventId = row.eventId || this.ids
      getEvent(eventId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改活动'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.eventId != null) {
            updateEvent(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addEvent(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const eventIds = row.eventId || this.ids
      this.$modal
        .confirm('是否确认删除活动编号为"' + eventIds + '"的数据项？')
        .then(function () {
          return delEvent(eventIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'event/event/export',
        {
          ...this.queryParams
        },
        `event_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
